(*
	file:    mytunesmail.scpt
	author:  davidfmiller http://www.fivevoltlogic.com/code/mytunes/
	version: 0.1
	about:   a script that runs 
	history: v0.1 thurs june 30, 2005
	             - initial version complete
*)

(* the mail account which will receive the mytunes emails *)
property MY_ACCOUNT : "" 

(* the name of the folder where the messages reside *)
property MY_MAILBOX : "" 

(* the subject of messages that should be processed must start with... *)
property MY_SUBJECT : "yamltunes:"

(* should messages be deleted once they've been processed? *)
property DELETE_PROCESSED : false

(* path to the ruby script that does the grunt work *)
property PATH_TO_RUBY_SCRIPT : "~/bin/yamltunesmail.rb"

(* the path to in your home folder where mail stores the "physical" files for your messages *)
property PATH_TO_EMAIL_MESSAGES : "~/Library/Mail/POP-name@domain.com@mail.server.com/INBOX.mbox/Messages/"

tell application "Mail"
	
	-- find all messages that are mytunes-related	
	set mymessages to every message in mailbox MY_MAILBOX of account MY_ACCOUNT whose subject starts with MY_SUBJECT

	repeat with m in mymessages
		
		set myid to (id of m) as string
		set mypath to PATH_TO_EMAIL_MESSAGES & myid & ".emlx"
		
		set cmd to "ruby " & PATH_TO_RUBY_SCRIPT & " " & mypath
		
		try
			set r to do shell script cmd
		on error e
			display alert "Error Processing YAMLTunes" message e
		end try
		
		if (DELETE_PROCESSED) then
			delete m
		end if
		
	end repeat
	
end tell